using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Admin2024.Domain.Entity.System
{

    public class AppRole : BaseEntity// app_role
    {
        public List<AppPermission> _appPermission = new List<AppPermission>();   
        public string RoleName { get; set; } = null!;// role_name
        public string? Description { get; set; }


        // 一般用于ORM工具初始化对象
        public AppRole()
        {

        }

        public AppRole(string roleName, string description)
        {
            RoleName = roleName;
            Description = description;
        }

        // 分配权限

        public void AllocatePermission(AppPermission appPermission)
        {
            _appPermission.Add(appPermission);
        }


        // 移除权限

        public void RemovePermission(AppPermission appPermission)
        {
            _appPermission.Remove(appPermission);
        }

        // 判断有没有权限

        public bool HasPermission(AppResource appResource, AppOperation appOperation)
        {
            return _appPermission.Any(x => x.AppResourceId == appResource.Id
            && x.AppOperationId == appOperation.Id);
        }

        // 判断有没有权限
        public bool HasPermission(Guid resourceId, Guid operationId)
        {
            return _appPermission.Any(x => x.AppResourceId == resourceId
            && x.AppOperationId == operationId);
        }
    }
}
